/* Dialog */
(function($) {
    if (!$) return;
    if (!window.Dialog) window.Dialog = {};
    // ----------------------------
    // Dialog 命名空间
    // Dialog.namespace('Dialog');
    Dialog.namespace = function (name, sep) {
        var s = name.split(sep || '.'),
            d = {},
            o = function (a, b, c) {
                if (c < b.length) {
                    if (!a[b[c]]) {
                        a[b[c]] = {};
                    }
                    d = a[b[c]];
                    o(a[b[c]], b, c + 1);
                }
            };
        o(window, s, 0);
        return d;
    };
    // 模块方法定义，其中callback为定义后需要附加执行的处理
    Dialog.define = function (name, value, callback) {
        var obj = this,
            item = name;
        if (name.indexOf('.') > 0) {
            var a = name.split('.');
            item = a.pop();
            var source = a.join('.');
            obj = Dialog.namespace(source);
        }
        if (obj[item]) return;
        obj[item] = value;
        if (callback) callback();
        // LeisuJS.debug('LeisuJS.define', name, 'info');
    };
    Dialog.temp = {
        panel: ['<div id="{id}" class="util-dialog"><div class="mask"></div>{dialog}</div>'].join(''),
        dialog: ['<div id="{id}" class="util-dialog-cda {className}">{html}</div>'].join(''),
        title: ['<div class="dialog-title"><div class="{model}"></div>{title}</div>'].join(''),
        content: ['<div class="dialog-content {className}">{content}</div>'].join(''),
        buttons: ['<div class="dialog-buttons"></div>'].join(''),
        button: ['<a class="dialog-button {className}">{text}</div>'].join(''),
        close: ['<i class="icon-close"></i>'].join('')
    };
    Dialog.define('Dialog.page', {
        alt: function (option) {
            var me = this;
            var meOption = {
                model: 'successl',
                panel: null,
                temp: '',
                align: 'left',
                id: '',
                className: '',
                title: '提示',
                close: '',
                buttons: '',
                radius: 5,
                speed: null
            }
            for (var i in option) {
                meOption[i] = option[i];
            }
            var title = Dialog.temp.title.substitute({'title': meOption.title,'model':meOption.model}),
                content = Dialog.temp.content.substitute({'content': meOption.temp, className: meOption.align}),
                dialogStr = $(Dialog.temp.dialog.substitute({
                    html: Dialog.temp.close + title + (meOption.temp == '' ? '' : content),
                    className: "alt " + meOption.className
                })),
                buttons = $(Dialog.temp.buttons);
            dialogStr.css({"border-radius": meOption.radius});
            if (meOption.buttons != '') {
                dialogStr.append(buttons);
            }
            var temp;
            if (meOption.panel == null) {
                temp = $(Dialog.temp.panel.substitute({dialog: '', id: meOption.id}));
                dialogStr.addClass('fixed');
                temp.append(dialogStr);
                $("body").append(temp);
            } else {
                temp = dialogStr;
                dialogStr.attr('id', meOption.id).addClass('absolute');
                meOption.panel.append(temp);
            }
            me.show(temp, meOption);
            if (meOption.buttons != '') {
                var _width = buttons.width() / meOption.buttons.length;
                $.each(meOption.buttons, function (i, item) {
                    var button = $(Dialog.temp.button.substitute({
                        text: item.text,
                        className: item.className
                    }));
                    if(item.link){
                        button.attr('href',item.link);
                    }
                    if (i > 0) {
                        button.width(_width - 1).addClass('bd-l');
                    } else {
                        button.width(_width);
                    }
                    button.click(function () {
                        if (typeof(item.onClick) == 'undefined') {
                            me.close(temp);
                        } else {
                            item.onClick(temp);
                        }
                    });
                    buttons.append(button);
                });
            }

            if (meOption.speed == null) {
                dialogStr.click(function (e) {
                    e.stopPropagation();
                });
                setTimeout(function () {
                    $("body").click(function () {
                        me.close(temp);
                    });
                }, 10);
            } else {
                setTimeout(function () {
                    me.close(temp);
                }, meOption.speed);
            }
            return temp;
        },
        pop: function (option) {
            var me = this;
            var meOption = {
                temp: '',
                id: '',
                className: '',
                close: '',
                radius: 0
            }
            for (var i in option) {
                meOption[i] = option[i];
            }
            var dialogStr = Dialog.temp.dialog.substitute({
                    html: Dialog.temp.close + meOption.temp,
                    className: "pop " + meOption.className
                }),
                temp = $(Dialog.temp.panel.substitute({dialog: dialogStr, id: meOption.id}));
            temp.find(">.util-dialog-cda.pop").css({"border-radius": meOption.radius});
            $("body").append(temp);
            me.show(temp, meOption);
            return temp;
        },
        setPositions: function (t) {
            if (!t.hasClass('show')) {
                t.css({'margin-left': -t.outerWidth() / 2, 'margin-top': -t.outerHeight() / 2});
                setTimeout(function () {
                    t.addClass('show');
                }, 1);
            } else {
                t.animate({'margin-left': -t.outerWidth() / 2, 'margin-top': -t.outerHeight() / 2});
            }
        },
        show: function (t, option) {
            var me = this,
                dialogCda = t.hasClass('util-dialog-cda') ? t : t.find('.util-dialog-cda');
            t.fadeIn();
            if (option.panel == undefined || option.panel == null) {
                t.siblings().not($(".util-dialog,script,link,audio")).addClass("filter-transition filter");
            }
            me.setPositions(dialogCda);
            dialogCda.find(">.icon-close").click(function () {
                Dialog.page.close(t);
                if (option.close) option.close();
            });
        },
        close: function (t) {
            t = (t == undefined ? $(".util-dialog") : t);
            var dialogCda = t.hasClass('util-dialog-cda') ? t : t.find('.util-dialog-cda');

            t.fadeOut();
            if ($(".util-dialog").length <= 1) {
                t.siblings().removeClass("filter");
            }
            setTimeout(function () {
                t.siblings().removeClass("filter-transition");
            }, 300);

            dialogCda.removeClass('show').addClass('hide');
            setTimeout(function () {
                t.remove();
            }, 300)
        }
    });
})(jQuery);


